package cn.aylog.aspecct;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class Log {

    @Pointcut("execution(* *..service.impl.*.*(..))")
    public void pt1(){}

    @Before("pt1()")
    public void before() {
        System.out.println("before...");
    }

    @After("pt1()")
    public void after() {
        System.out.println("after...");
    }

    @AfterThrowing(value = "pt1()", throwing = "ex")
    public void afterThrowing(Exception ex) {
        System.out.println("afterThrowing...");
        ex.printStackTrace();
    }

    @AfterReturning(value = "pt1()", returning = "result")
    public void afterReturn(Object result) {
        System.out.println("afterReturn..." + result);
    }

    @Around("pt1()")
    public void around(ProceedingJoinPoint pjp) {
        Object[] args = pjp.getArgs();
        System.out.println("around--" + Arrays.asList(args));
        try {
            Object result = pjp.proceed(args);
            System.out.println("around result : " + result);
        } catch (Throwable throwable) {
            System.out.println("异常通知。。。");
            throwable.printStackTrace();
        }
        System.out.println("around 最终通知。。。");
    }
}
